package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工控制器
 * @author GY
 * @version V1.0
 * @date 2025-01-12 9:05
 */
@Slf4j
@RestController
@RequestMapping("/emps")
@RequiredArgsConstructor
public class EmpController {

    private final EmpService empService;

    /**
     * 2.1员工条件分页查询
     */
    @GetMapping
    public Result pageQuery(EmpQueryParam empQueryParam){
        log.info("empQueryParam :{}", empQueryParam );
        PageResult<Emp> pageResult = empService.pageQuery(empQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 2.2根据ID删除员工
     */
    @DeleteMapping
    public Result deleteByIds(@RequestParam List<Integer> ids){
        log.info("ids:{}",ids);
        empService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 2.3新增员工和工作经历
     */
    @PostMapping
    public Result save(@RequestBody Emp emp){
        log.info("emp:{}",emp);
        empService.saveAndExprList(emp);
        return Result.success();
    }

    /**
     * 2.4根据ID查询员工信息和工作经历
     */
    @GetMapping("/{id}")
    public Result findByIdAndExprList(@PathVariable Integer id){
        log.info("id:{}",id);
        Emp emp = empService.findByIdAndExprList(id);
        return Result.success(emp);
    }

    /**
     * 2.5修改员工信息和工作经历
     */
    @PutMapping
    public Result updateEmpAndExprList(@RequestBody Emp emp){
        log.info("emp:{}",emp);
        empService.updateEmpAndExprList(emp);
        return Result.success();
    }

    /**
     * 查询全部
     */
    @GetMapping("/list")
    public Result findAll(Emp emp){
        log.info("emp:{}",emp);
        return Result.success(empService.findAll(emp));
    }
}
